#include "DigitalPutOption.h"
#include "MonteCarloPricer.h"
#include "matlib.h"
#include "testing.h"


DigitalPutOption::DigitalPutOption() :
    strike(0.0),
    maturity(0.0) {
}

double DigitalPutOption::payoff(double stockAtMaturity) const {
    if (stockAtMaturity >= strike) {
        return 0.0;
    }
    else {
        return 1.0;
    }
}

double DigitalPutOption::getMaturity() const {
    return maturity;
}


//////////////////////////
//
//  Test the call option class
//
//
//////////////////////////

static void testDigitalPutOptionPayoff() {
    DigitalPutOption dpo;
    dpo.strike = 105.0;
    dpo.maturity = 2.0;
    ASSERT_APPROX_EQUAL(dpo.payoff(110.0), 0, 0.001);
    ASSERT_APPROX_EQUAL(dpo.payoff(100.0), 1, 0.001);
}

static void testDigitalPutOptionPrice() {
    rng("default");

    BlackScholesModel m;
    m.volatility = 0.1;
    m.riskFreeRate = 0.05;
    m.stockPrice = 100.0;
    m.drift = 0.1;

    DigitalPutOption dpo;
    dpo.strike = 1100000;
    dpo.maturity = 2;

    DigitalPutOption dpo1;
    dpo1.strike = 0.1;
    dpo1.maturity = dpo.maturity;

    // our pricer can price puts and calls
    MonteCarloPricer pricer;
    double price0 = pricer.price(dpo, m);
    ASSERT_APPROX_EQUAL(price0, 1.0, 0.1);
    double price1 = pricer.price(dpo1, m);
    ASSERT_APPROX_EQUAL(price1, 0.0, 0.1);
}



void testDigitalPutOption() {

    TEST(testDigitalPutOptionPayoff);
    TEST(testDigitalPutOptionPrice);
}